package com.kgcx.order.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.api.ApiContant;
import com.kgcx.common.rabbitmq.bean.Message;
import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.order.domain.Order;
import com.kgcx.order.domain.Order4Flat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 何凯
 * @version 0.1
 * @description 给监管平台发送消息
 * @time 2017/12/8 10:17
 * @since 0.1
 */
@Slf4j
@Component
public class OpcProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送订单创建消息给监管平台
     * @param order
     */
     public void sendOrderCreate(Order order) {
        log.info(String.format("查询订单创建的MQ消息，评价信息id[%s]。", order.getOrderNo()));
        Message msg = Message.buildMessage(ApiContant.ORDER_CREATE, Message.FLAG_ADD, order);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
        log.info(String.format("msgId[%s]。", msg.getId()));
     }
     /**
     * 发送订单匹配成功消息给监管平台
     * @param order
     */
     public void sendOrderMatch(Order4Flat order) {
        log.info(String.format("查询订单创建的MQ消息，评价信息id[%s]。", order.getDriverLicenseId()));
        Message msg = Message.buildMessage(ApiContant.ORDER_MATCH, Message.FLAG_ADD, order);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
        log.info(String.format("msgId[%s]。", msg.getId()));
     }
      /**
     * 发送订单取消消息给监管平台
     * @param order
     */
     public void sendOrderCancel(Order order) {
        log.info(String.format("查询订单创建的MQ消息，评价信息id[%s]。", order.getOrderNo()));
        Message msg = Message.buildMessage(ApiContant.ORDER_CANCEL, Message.FLAG_ADD, order);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
        log.info(String.format("msgId[%s]。", msg.getId()));
     }
      /**
     * 发送经营出发消息给监管平台
     * @param order
     */
     public void sendOperateDepart(Order4Flat order) {
        log.info(String.format("查询订单创建的MQ消息，评价信息id[%s]。", order.getDriverLicenseId()));
        Message msg = Message.buildMessage(ApiContant.ORDER_DEPART, Message.FLAG_ADD, order);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
        log.info(String.format("msgId[%s]。", msg.getId()));
     }
      /**
     * 发送经营到达消息给监管平台
     * @param order
     */
     public void sendOperateArrived(Order order) {
        log.info(String.format("查询订单创建的MQ消息，评价信息id[%s]。", order.getOrderNo()));
        Message msg = Message.buildMessage(ApiContant.ORDER_ARRIVED, Message.FLAG_ADD, order);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
        log.info(String.format("msgId[%s]。", msg.getId()));
     }
}
